package cz.muni.fi.pa165.web;

import cz.muni.restinterface.CustomerDTO;
import cz.muni.service.CustomerServiceLocal;
import java.util.List;
import javax.ejb.EJB;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

/**
 *
 * @author Thomas
 */
@UrlBinding("/Manager-Customer.{$event}")
public class ManagerCustomerActionBean implements ActionBean {
    
    @EJB(mappedName="java:global/DeliveryCompany-web/CustomerService!cz.muni.service.CustomerServiceLocal")
    private CustomerServiceLocal customerService;
    private ActionBeanContext context;
    
    @ValidateNestedProperties({
        @Validate(field="firstName", required=true),
        @Validate(field="lastName", required=true),
        @Validate(field="street", required=true),
        @Validate(field="streetNumber", required=true),
        @Validate(field="postalCode", required=true),
        @Validate(field="phoneNumber", required=true),
        @Validate(field="email", required=true),
    })
    private CustomerDTO customer;
    private List<CustomerDTO> customers;

    public CustomerDTO getCustomer() {
        return customer;
    }

    public void setCustomer(CustomerDTO customer) {
        this.customer = customer;
    }
    
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }
    
    public List<CustomerDTO> getCustomers() { 
        return customerService.findAll();
    }
    
    @DefaultHandler
    public Resolution df() {
        return new ForwardResolution("/manager_customers.jsp");
    }

    @HandlesEvent("add")
    public Resolution add() {
        customerService.create(customer);
        return new RedirectResolution("/manager_customers.jsp");
    }
    
    @DontValidate
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "update"})
    public void loadCustomerFromDatabase() {
        String ids = context.getRequest().getParameter("customer.id");
        if (ids == null) return;
        customer = customerService.find(Long.parseLong(ids));
    }
 
    @DontValidate
    public Resolution edit() {
        return new ForwardResolution("/editCustomer.jsp");
    }
 
    @DontValidate
    public Resolution update() {
        customerService.update(customer);
        return new RedirectResolution("/manager_customers.jsp");
    }
    
    @DontValidate
    public Resolution delete() {
        try {
            customerService.delete(customer);
        } catch (Exception e) {
            return new RedirectResolution("/manager_error.jsp");
        }
        return new RedirectResolution("/manager_customers.jsp");
    }
}
